import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

class KlDigitview extends StatefulWidget {
  KlDigitview({
    super.key,
    required this.digitMap,
    this.isTheLast = false,
    this.bgColor = Colors.white,
    this.rightNumColor = Colors.white,
    this.oddIndex = 0,
  });
  Map digitMap;
  bool? isTheLast;
  Color? bgColor;
  Color? rightNumColor;
  int? oddIndex;
  @override
  State<KlDigitview> createState() => _KlDigitviewState();
}

class _KlDigitviewState extends State<KlDigitview> {
  late Map _digitMap = {};
  late List<Widget> _rowViews = [];
  late List _numbers = []; //号码：1-9、A-M(i除外)
  late String _title = ''; // '1st Digit'...;

  late final bool _isTheLast = widget.isTheLast ?? false;
  double cellW = 25.0;
  double cellH = 40.0;

  late Color _bgColor = widget.bgColor ?? Colors.white;
  late Color _rightNumColor = widget.rightNumColor ?? Colors.white;

  @override
  void initState() {
    super.initState();
    configData();
  }

  void configData() {
    cellH = _isTheLast ? cellH * 2 : 40.0;
    _digitMap = widget.digitMap;
    _numbers = _digitMap['nums'];
    _title = _digitMap['title'] ?? '';
    for (int i = 0; i < _numbers.length; i++) {
      Map element = _numbers[i];
      var codeString = element['code'];
      var isRightNum = element['isRight'];
      _rowViews.add(rowItem(i, codeString, cellW, cellH, isRight: isRightNum));
    }
  }

  @override
  Widget build(BuildContext context) {
    // configData();
    double viewH = _title.isEmpty ? cellH : cellH * 2;
    return SizedBox(
      height: viewH,
      width: cellW * _numbers.length,
      child: Column(children: [
        _title.isEmpty
            ? Container()
            : Container(
                height: cellH,
                width: cellW * _numbers.length,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border(
                    top: BorderSide(color: HexColor('#dddddd'), width: 1),
                    bottom: BorderSide(color: HexColor('#dddddd'), width: 1),
                    right: BorderSide(color: HexColor('#dddddd'), width: 1),
                  ),
                ),
                child: Text(
                  _title,
                  style: const TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: 12,
                    color: Colors.black,
                  ),
                ),
              ),
        SizedBox(
          height: cellH,
          width: cellW * _numbers.length,
          child: Row(
            children: _rowViews,
          ),
        ),
      ]),
    );
  }

  Widget rowItem(int index, String num, double cellW, double cellH,
      {bool isRight = false}) {
    Color _numColor = isRight ? _rightNumColor : _bgColor;
    Color _textColor = isRight ? Colors.white : Colors.black;

    return Container(
      height: cellH,
      width: cellW,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: _bgColor,
        border: Border(
          bottom: BorderSide(color: HexColor('#dddddd'), width: 1),
          right: BorderSide(color: HexColor('#dddddd'), width: 1),
        ),
      ),
      child: Container(
        width: 20,
        height: 20,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: _numColor,
          borderRadius: const BorderRadius.all(Radius.circular(20 * 0.5)),
        ),
        child: Text(
          num,
          style: TextStyle(
            fontWeight: FontWeight.w600,
            fontSize: 12,
            color: _textColor,
          ),
        ),
      ),
    );
  }
}
